package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlarmQueryDTO;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public void updateAlertData(AlertData alertData) {
        AlertData alertData1 = selectAlertDataById(alertData.getId());
        String name = alertData1.getNickname();
        String functionId = alertData1.getFunctionId();
        List<AlertData> list = list(Wrappers.<AlertData>lambdaQuery().eq(AlertData::getNickname, name).eq(AlertData::getFunctionId, functionId).eq(AlertData::getStatus, 0));
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId;
        if (ObjectUtils.isNotEmpty(loginUser)) {
            userId = loginUser.getUserId();
        } else {
            userId = 0L;
        }
        list.forEach(item -> {
            item.setStatus(1);
            item.setProcessingTime(alertData.getProcessingTime());
            item.setProcessorId(userId);
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            item.setProcessorName(sysUser.getUserName());
            item.setProcessingResult(alertData.getProcessingResult());
            updateById(item);
        });


    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return alertDataMapper.deleteById(id);
    }

    @Override
    public Page<AlertData> getAlertDataList(AlarmQueryDTO alertData) {
        Page<AlertData> alertDataPage = new Page<>(alertData.getPageNum(), alertData.getPageSize());
        QueryWrapper<AlertData> queryWrapper = new QueryWrapper<>();
        if (alertData.getStartTime() != null && alertData.getEndTime() != null) {
            queryWrapper.between("processing_time", alertData.getStartTime(), alertData.getEndTime());
        }
        if (alertData.getStatus() != null) {
            queryWrapper.eq("status", alertData.getStatus());
        }
        if (alertData.getDeviceName() != null) {
            queryWrapper.like("device_name", alertData.getDeviceName());
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            queryWrapper.eq("user_id", loginUser.getUserId());
        }
        alertDataPage = page(alertDataPage, queryWrapper);
        return alertDataPage;
    }

    @Override
    public Page<AlertData> pageQueryAlertData(String functionId, Long startTime, Long endTime, String iotId, int pageNum, int pageSize) {
        Page<AlertData> alertDataPage = new Page<>(pageNum, pageSize);
        QueryWrapper<AlertData> alertDataQueryWrapper = new QueryWrapper<>();
        if (startTime != null && endTime != null) {
            alertDataQueryWrapper.between("processing_time", LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        }
        if (iotId != null) {
            alertDataQueryWrapper.eq("iot_id", iotId);
        }
        if (functionId != null) {
            alertDataQueryWrapper.eq("function_id", functionId);
        }
        alertDataPage = page(alertDataPage, alertDataQueryWrapper);

        return alertDataPage;
    }
}
